{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bb871b1d",
   "metadata": {},
   "source": [
    "# 非参数检验"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd6bdf57",
   "metadata": {},
   "source": [
    "## 检验单个总体位置\n",
    "1. 使用的统计量是符合二项分布的\n",
    "2. 因此显著性水平p值即为二项分布pmf的响应x值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a94380e4",
   "metadata": {},
   "source": [
    "## 活性细菌总数符号检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "77e303a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.171875"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from scipy import stats\n",
    "import math\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "def sign_test(data, miu, alpha=0.05):\n",
    "    n = len(data)\n",
    "    pos = 0\n",
    "    neg = 0\n",
    "    for i in range(len(data)):\n",
    "        if data[i] > miu:\n",
    "            pos +=1\n",
    "        else:\n",
    "            neg +=1\n",
    "    if (pos+neg) != n:\n",
    "        return -1\n",
    "    return 1 - stats.binom.cdf(max(pos,neg),n,0.5)\n",
    "\n",
    "bacteria_data = [41,33,43,52,46,37,44,39,53,30]\n",
    "\n",
    "\n",
    "p = sign_test(bacteria_data, 40)\n",
    "p"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b63f00c9",
   "metadata": {},
   "source": [
    "# 比较两个总体\n",
    "1. 威尔克森秩和检验<br>\n",
    "原假设$H_0$: 两总体统计量相等<br>\n",
    "备择假设$H_1$： 两总体统计量不等，一个大于另一个为单边检验；两个不等为双边检验<br>\n",
    "2. 当$n_1,n_2\\le10$时，使用查表的方法，表格来自于古典概型\n",
    "3. 当$n_1,n_2>10$时，使用标准正态分布Z作为枢轴统计量\n",
    "$$\n",
    "\\begin{array}{ll}\n",
    "    E[T_1] = \\frac{n_1n_2+n_1(n_1+1)}{2} \\\\\n",
    "    V[T_1] = \\frac{n_1n_2+n_1(n_1+n_2+1)}{2} \\\\\n",
    "    Z = \\frac{T_1-E[T_1]}{\\sqrt{V[T_1]}}\n",
    "\\end{array}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b870c24",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
